package com.tanhua.dubbo.api;


import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public void save(Video video) {
        video.setVid(idWorker.getNextId("video"));
        mongoTemplate.save(video);
    }

    @Override
    //根据vid查数据
    public List<Video> findVideosByPids(List<Long> vids1) {
        Query query = Query.query(Criteria.where("vid").in(vids1)).with(Sort.by(Sort.Order.desc("created")));
        List<Video> list = mongoTemplate.find(query, Video.class);
        return list;
    }

    @Override
    //根据推荐列表查数据
    public List<Video> queryVideos(int i, Integer pagesize) {
        Query query = new Query().with(Sort.by(Sort.Order.desc("created"))).skip((i - 1) * pagesize).limit(pagesize);
        List<Video> list = mongoTemplate.find(query, Video.class);
        return list;
    }

    @Override
    //查找个人视频
    public List<Video> findByUserVideos(Integer page, Integer pagesize, Long uid) {
        Query query =Query.query(Criteria.where("userId").is(uid)).with(Sort.by(Sort.Order.desc("created")));
        List<Video> list = mongoTemplate.find(query, Video.class);
        return list;


    }
}
